//
//  ListNewsItemView.m
//  BaoVn
//
//  Created by Le Anh Tai on 11/8/14.
//  Copyright (c) 2014 Le Anh Tai. All rights reserved.
//

#import "ListNewsItemView.h"

@implementation ListNewsItemView

- (instancetype)init
{
    if (self = [super init])
    {
        _imageWidthWei = 1;
        _contentWidthWei = 2;
    }
    
    return self;
}

- (void)setFrame:(CGRect)frame
{
    [super setFrame:frame];
    
    if (CGSizeEqualToSize(frame.size, CGSizeZero))
    {
        return;
    }
    
    NSInteger left = self.marginLeft;
    NSInteger widWithoutMargin = self.width - self.marginLeft - self.marginRight - self.paddingImageAndContent;
    NSInteger widImage = (_imageWidthWei/(_imageWidthWei + _contentWidthWei))*widWithoutMargin;
    NSInteger maxHeiTitle;
    
    if (_hideImage)
    {
        self.imageView.hidden = YES;
    }
    else
    {
        self.imageView.hidden = NO;
        self.imageView.frame = RECT(left, self.marginTop, widImage, self.height - self.marginTop - self.marginBottom);
        
        left = self.imageView.right + self.paddingImageAndContent;
    }
    
    [self.createdDateLabel sizeToFit];
    self.createdDateLabel.origin = POINT(left, self.height - self.createdDateLabel.height - self.marginBottom);
    
    self.titleLabel.frame = RECT(left, self.marginTop, self.width - left - self.marginRight, 500);
    [self.titleLabel sizeToFit];
    maxHeiTitle = self.createdDateLabel.top - self.marginTop - self.paddingImageAndContent;
    if (self.titleLabel.height > maxHeiTitle)
    {
        self.titleLabel.height = maxHeiTitle;
    }
    
    NSInteger minHeiDescription = 15;
    if (self.createdDateLabel.top - self.titleLabel.bottom > minHeiDescription)
    {
        self.descriptionLabel.hidden = NO;
        self.descriptionLabel.frame = RECT(left, self.titleLabel.bottom, self.titleLabel.width, self.createdDateLabel.top - self.titleLabel.bottom);
    }
    else
    {
        self.descriptionLabel.hidden = YES;
    }    
}

@end
